#include <math.h>

#define speakerOut  2

int pitchval = 1;
int val = 0;
int wait = 100;

int freq,t;

float noteval;
//note values
float C     = 130.8;
float CS    = 138.6;
float D     = 146.8;
float DS    = 155.6;
float E     = 164.8;
float F     = 174.6;
float FS    = 185;
float G     = 196;
float GS    = 207.7;
float A2    = 220;
float A2S   = 233.1;
float B2    = 246.9;
float C2    = 261.6;
float C2S   = 277.2;
float D2    = 293.7;
float D2S   = 311.1;
float E2    = 329.6;
float F2    = 349.2;
float F2S   = 370;
float G2    = 392;
float G2S   = 415.3;
float A3    = 440;


void setup() {
  pinMode(speakerOut, OUTPUT);
  //Serial.begin(9600);
  start();
}

void loop() {

    delay(3000);
    hinderniss();
    delay(5000);
    ende();
}

void freqout(int freq, int t) 
{ 
  int hperiod;     //calculate 1/2 period in us 
  long cycles, i; 
  hperiod = (500000 / ((freq - 7) * pitchval));             // subtract 7 us to make up for digitalWrite overhead - determined empirically 
  // calculate cycles 
  cycles = ((long)freq * (long)t) / 1000;    // calculate cycles 

  for (i=0; i<= cycles; i++){              // play note for t ms  
    digitalWrite(speakerOut, HIGH);  
    delayMicroseconds(hperiod); 
    digitalWrite(speakerOut, LOW);  
    delayMicroseconds(hperiod - 1);     // - 1 to make up for fractional microsecond in digitaWrite overhead 
  } 
}

void HappyBirthday(){
    //Happy Bithday you
    noteval = C2;
    freqout((int)noteval,300);  
    noteval = C2;
    freqout((int)noteval,300);

    noteval = D2;
    freqout((int)noteval,500);

    noteval = C2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = F2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = E2;
    freqout((int)noteval,1200);
    delay(wait*2);

    //Happy Bithday to you
    noteval = C2;
    freqout((int)noteval,300);  
    noteval = C2;
    freqout((int)noteval,300);

    noteval = D2;
    freqout((int)noteval,500);

    noteval = C2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = G2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = F2;
    freqout((int)noteval,1200);
    delay(wait*2);


    noteval = C2;
    freqout((int)noteval,300);
    noteval = C2;
    freqout((int)noteval,300);

    noteval = C2*2;
    freqout((int)noteval,500);

    noteval = A2*2;
    freqout((int)noteval,500);

    noteval = F2;
    freqout((int)noteval,300);
    noteval = F2;
    freqout((int)noteval,300);

    noteval = E2;
    freqout((int)noteval,500);

    noteval = D2;
    freqout((int)noteval,500);
    delay(wait);

    noteval = B2*2;
    freqout((int)noteval,300);  
    noteval = B2*2;
    freqout((int)noteval,300);
    delay(wait);

    noteval = A2*2;
    freqout((int)noteval,500);
    delay(wait);
    noteval = F2;
    freqout((int)noteval,500);
    delay(wait);
    noteval = G2;
    freqout((int)noteval,500);
    delay(wait);
    noteval = F2;
    freqout((int)noteval,1200);  
  }
  
  void start(){
    freqout(800,150);
    delay(25);
    freqout(1200,150);
    delay(50);
    freqout(1600,230);
    }
  void ende(){
    freqout(1600,150);
    delay(50);
    freqout(1200,150);
    delay(25);
    freqout(800,230);
    }
  void hinderniss(){
      freqout(2000,50);
      delay(50);
      freqout(2000,50);
    }
